package com.cool.apple.wechat.Controller;

import com.cool.apple.wechat.Utils.CheckUtil;
import com.cool.apple.wechat.Utils.InitMsgContentUtil;
import com.cool.apple.wechat.Utils.MessageUtil;
import com.cool.apple.wechat.Utils.MsgTypeParam;
import org.dom4j.DocumentException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;

@Controller
public class CheckTokenController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    @ResponseBody
    public String gettoken(String signature,String timestamp,
                           String nonce,String echostr,
                           HttpServletResponse response,
                           HttpServletRequest request) throws IOException, DocumentException {
        CheckUtil checkUtil = new CheckUtil();
        boolean checkSignature = checkUtil.checkSignature(signature, timestamp, nonce);
        if(checkSignature){
            return echostr;
        }else {
            return null;
        }
    }

    @RequestMapping(value = "/index",method = RequestMethod.POST)
    @ResponseBody
    public void postMessage(HttpServletResponse response,
                      HttpServletRequest request) throws IOException, DocumentException {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Map<String, String> map = MessageUtil.xmlToMap(request);

//            System.out.println(content+"/"+fromUserName+"/"+toUserName+"/"+msgType+"/"+createTime);
            rabbitTemplate.convertAndSend
                    ("TestDirectExchange", "TestDirectRouting",map);
            //回复文本消息
            String message = null;
            String fromUserName = map.get("FromUserName");
            String toUserName = map.get("ToUserName");
            //消息类型
            String createTime = map.get("CreateTime");
            String msgType = map.get("MsgType");
            String content = map.get("Content");
            //消息类型判断
            if (MsgTypeParam.MESSAGE_TEXT.equals(msgType)) {
                String text = "王霞我爱你"+content;
                //调用初始化文本消息方法
                message = InitMsgContentUtil.initText(toUserName, fromUserName, text);
                out.print(message);
            }else {
                String text="请输入文字";
                message = InitMsgContentUtil.initText(toUserName, fromUserName, text);
                out.print(message);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }


    }
}
